package com.tg.app.activity.device;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.text.TextUtils;

import com.appbase.custom.config.ApiUrl;
import com.appbase.custom.constant.CommonConstants;
import com.base.BaseActivity;
import com.tange.base.toolkit.StringUtils;
import com.tange.module.device.feature.DeviceFeature;
import com.tange.module.device.feature.DeviceFeatureObjectBox;
import com.tg.app.R;
import com.tg.app.activity.CloudServiceActivity;
import com.tg.app.activity.TimeZoneActivity;
import com.tg.app.activity.base.WebBaseActivity;
import com.tg.app.activity.device.add.ResetDeviceActivity;
import com.tg.app.activity.device.add.ScanQrcodeActivity;
import com.tg.app.activity.device.settings.AlertSoundActivity;
import com.tg.app.activity.device.settings.AutoTrackingActivity;
import com.tg.app.activity.device.settings.BatterySleepActivity;
import com.tg.app.activity.device.settings.ChangeDeviceWifiActivity;
import com.tg.app.activity.device.settings.CloudResolutionActivity;
import com.tg.app.activity.device.settings.DeviceInfoActivity;
import com.tg.app.activity.device.settings.DoubleLightActivity;
import com.tg.app.activity.device.settings.DoubleLightAutoActivity;
import com.tg.app.activity.device.settings.LightSettingsActivity;
import com.tg.app.activity.device.settings.MoreSettingsActivity;
import com.tg.app.activity.device.settings.NetworkSettingsActivity;
import com.tg.app.activity.device.settings.NightVisionActivity;
import com.tg.app.activity.device.settings.PIRSensitivityActivity;
import com.tg.app.activity.device.settings.PlaySleep4GActivity;
import com.tg.app.activity.device.settings.RebootActivity;
import com.tg.app.activity.device.settings.RecordCheckActivity;
import com.tg.app.activity.device.settings.RecordModeActivity;
import com.tg.app.activity.device.settings.RotateActivity;
import com.tg.app.activity.device.settings.SDCardStateActivity;
import com.tg.app.activity.device.settings.SmartSpeakerActivity;
import com.tg.app.activity.device.settings.SolarVideoSettingsActivity;
import com.tg.app.activity.device.settings.SpeakerVolumeActivity;
import com.tg.app.activity.device.settings.VideoSettingsActivity;
import com.tg.app.camera.Camera;
import com.tg.app.helper.ActivityHelper;
import com.tg.app.helper.DeviceHelper;
import com.tg.app.helper.TGGlobalConfigHelper;
import com.tg.app.util.ObjectBoxUtil;
import com.tg.app.util.UriUtil;
import com.tg.appcommon.android.AbAppUtil;
import com.tg.appcommon.android.ResourcesUtil;
import com.tg.appcommon.android.TGLog;
import com.tg.data.bean.DeviceItem;
import com.tg.data.bean.DeviceSettingsInfo;
import com.tg.data.helper.DeviceItemHelper;

import io.objectbox.Box;

public class DeviceSettingHelper {
    private static final String TAG = "DeviceSettingHelper";
    public static final int DAY_NIGHT_AUTO_ACTIVITY = 34;
    public static final int DOUBLE_LIGHT_ACTIVITY = 35;
    public static final int RESET_DEVICE_ACTIVITY = 36;
    public static void doJump(Activity context, int whichActivity, DeviceSettingsInfo deviceSettingsInfo, DeviceItem deviceItem) {
        doJump(context, whichActivity, deviceSettingsInfo, deviceItem, null);
    }

    public static void doJump(Activity context, int whichActivity, DeviceSettingsInfo deviceSettingsInfo, DeviceItem deviceItem, String title) {
        Intent intent = new Intent();
        doJump(intent, context, whichActivity, deviceSettingsInfo, deviceItem, title);
    }

    public static void doJump(Intent intent, Activity context, int whichActivity, DeviceSettingsInfo deviceSettingsInfo, DeviceItem deviceItem, String title) {
        if (deviceItem == null) {
            TGLog.e(TAG, "doJump: deviceItem == null");
            return;
        }
        deviceSettingsInfo.hasDeviceUpdate = deviceItem.is_upgrade == 1;
        deviceSettingsInfo.device_type = deviceItem.device_type;
        intent.putExtra(DeviceSettingsActivity.EXT_DEVICE_INFO, deviceSettingsInfo);
        intent.putExtra(Camera.EXT_UUID, deviceItem.uuid);
        intent.putExtra(BaseActivity.SETTING_ITEM_TITLE, title);
        if (deviceItem.ai_server_data != null) {
            deviceSettingsInfo.ai_service = true;
        }
        if (TextUtils.isEmpty(deviceSettingsInfo.version)) {
            deviceSettingsInfo.version = deviceItem.current_version;
        }
        switch (whichActivity) {
            case DeviceSettingsActivity.CUSTOMER_DEVICE_SETTING:
                DeviceHelper.openCustomerService(deviceItem, context);
                break;
            case DeviceSettingsActivity.AUTO_TRACKING_ACTIVITY:
                intent.setClass(context, AutoTrackingActivity.class);
                break;
            case DeviceSettingsActivity.SMART_SPEAKERS_SETTING:
                intent.setClass(context, SmartSpeakerActivity.class);
                intent.putExtra(CommonConstants.EXT_DEVICE_ITEM, deviceItem);
                break;

            case DeviceSettingsActivity.NETWORK_SETTING:
                //设备信息
                intent.setClass(context, NetworkSettingsActivity.class);
                intent.putExtra(CommonConstants.EXT_DEVICE_ITEM, deviceItem);
                intent.putExtra(DeviceInfoActivity.EXT_DEVICE_NAME_TYPE, DeviceHelper.getDeviceTypeName(deviceItem));
                break;
            case DeviceSettingsActivity.MORE_SETTING:
                //设备信息
                intent.setClass(context, MoreSettingsActivity.class);
                intent.putExtra(CommonConstants.EXT_DEVICE_ITEM, deviceItem);
                intent.putExtra(DeviceInfoActivity.EXT_DEVICE_NAME_TYPE, DeviceHelper.getDeviceTypeName(deviceItem));
                break;
            case DeviceSettingsActivity.DEVICE_INFO_ACTIVITY:
                //设备信息
                intent.setClass(context, DeviceInfoActivity.class);
                intent.putExtra(CommonConstants.EXT_DEVICE_ITEM, deviceItem);
                intent.putExtra(DeviceInfoActivity.EXT_DEVICE_NAME_TYPE, DeviceHelper.getDeviceTypeName(deviceItem));
                break;

            case DeviceSettingsActivity.RECORD_MODE_ACTIVITY:
                intent.setClass(context, RecordModeActivity.class);
                break;

            case DeviceSettingsActivity.CLOUD_RESOLUTION_ACTIVITY:
                intent.setClass(context, CloudResolutionActivity.class);
                break;

            case DeviceSettingsActivity.RECORD_CHECK_ACTIVITY:
                intent.setClass(context, RecordCheckActivity.class);
                break;

            case DeviceSettingsActivity.SDCARD_STATE_ACTIVITY:
                intent.setClass(context, SDCardStateActivity.class);
                break;

            case DeviceSettingsActivity.ROTATE_ACTIVITY:
                intent.setClass(context, RotateActivity.class);
                break;

            case DeviceSettingsActivity.DEVICE_SHARE_LIST_ACTIVITY:
                intent.putExtra(DeviceSettingsActivity.EXT_DEVICE_INFO, deviceSettingsInfo);
                String url = TGGlobalConfigHelper.getInstance().getShareUrl();
                if (StringUtils.isEmpty(url)){
                    url = ApiUrl.APP_SHARE_LIST_PAGE;
                }
                UriUtil.openServePlayImpl(context, intent, url, "", 0);
                return;

            case DeviceSettingsActivity.CLOUD_SERVICE_ACTIVITY:
                if (DeviceHelper.isPassiveDoorBellDevice(deviceItem)){
                    intent.putExtra(CloudServiceActivity.EXT_CLOUD_SERVICE_TYPE, CloudServiceActivity.EXT_DOORBELL_SERVICE_TYPE_STORAGEORCAR);
                }

                intent.setClass(context, CloudServiceActivity.class);
                context.startActivityForResult(intent, 0);
                return;

            case DeviceSettingsActivity.TIME_ZONE_ACTIVITY:
                intent.setClass(context, TimeZoneActivity.class);
                break;

            case DeviceSettingsActivity.NIGHT_VISION_ACTIVITY:
                intent.setClass(context, NightVisionActivity.class);
                break;

            case DeviceSettingsActivity.LIGHT_SETTINGS_ACTIVITY:
                intent.putExtra(CommonConstants.EXT_DEVICE_ITEM, deviceItem);
                intent.setClass(context, LightSettingsActivity.class);
                break;

            case DeviceSettingsActivity.AI_SERVICE_ACTIVITY:
                intent.setClass(context, CloudServiceActivity.class);
                if (DeviceItemHelper.isSuperAiService(deviceItem)){
                    intent.putExtra(CloudServiceActivity.EXT_CLOUD_SERVICE_TYPE, CloudServiceActivity.EXT_CLOUD_SERVICE_TYPE_SUPER_AI);
                }else{
                    intent.putExtra(CloudServiceActivity.EXT_CLOUD_SERVICE_TYPE, CloudServiceActivity.EXT_CLOUD_SERVICE_TYPE_AI);
                }

                context.startActivityForResult(intent, 0);
                return;

            case DeviceSettingsActivity.CHANGE_DEVICE_WIFI_ACTIVITY:
                intent.putExtra(CommonConstants.EXT_DEVICE_ITEM, deviceItem);
                intent.setClass(context, ChangeDeviceWifiActivity.class);
                break;
            case RESET_DEVICE_ACTIVITY:
                intent.setClass(context, ResetDeviceActivity.class);
                String uuid = deviceSettingsInfo.uuid;
                if (StringUtils.isEmpty(uuid)){
                    uuid = deviceItem.uuid;
                }
                ActivityHelper.jump2ResetDeviceActivity(context, uuid, intent);
                return;
            case DeviceSettingsActivity.ADD_CHOOSE_ACTIVITY:
                intent.setClass(context, ScanQrcodeActivity.class);
                break;

            case DeviceSettingsActivity.SIM_SERVICE_ACTIVITY:
                if (!StringUtils.isEmpty(deviceItem.foreignServiceHook)){
                    AbAppUtil.openServeByBrowser(context, deviceItem.foreignServiceHook);
                }else{
                    intent.setClass(context, CloudServiceActivity.class);
                    intent.putExtra(CloudServiceActivity.EXT_CLOUD_SERVICE_TYPE, CloudServiceActivity.EXT_CLOUD_SERVICE_TYPE_SIM);
                    context.startActivityForResult(intent, 0);
                }
                return;
            case DeviceSettingsActivity.DEVICE_SLEEP_ACTIVITY:
                intent.setClass(context, PlaySleep4GActivity.class);
                break;
            case DeviceSettingsActivity.ALERT_SOUND_ACTIVITY:
                intent.putExtra(CommonConstants.EXT_DEVICE_ITEM, deviceItem);
                intent.setClass(context, AlertSoundActivity.class);
                break;

            case DeviceSettingsActivity.VIDEO_SETTINGS_ACTIVITY:
                intent.putExtra(CommonConstants.EXT_DEVICE_ITEM, deviceItem);
                if (DeviceHelper.isSolar(deviceItem)){
                    intent.setClass(context, SolarVideoSettingsActivity.class);
                }else{
                    intent.setClass(context, VideoSettingsActivity.class);
                }
                break;
            case DeviceSettingsActivity.CAR_SERVICE_ACTIVITY:
                intent.putExtra(WebBaseActivity.KEY_WEBURL, ApiUrl.CAR_DETAIL_PAGE);
                intent.putExtra(WebBaseActivity.KEY_WEBTITLE, ResourcesUtil.getString(R.string.settings_device_car));
                intent.putExtra(CloudServiceActivity.EXT_CLOUD_SERVICE_TYPE, 0);
                intent.setClass(context, CloudServiceActivity.class);
                context.startActivityForResult(intent, 0);
                return;
            case DeviceSettingsActivity.BATTERY_SLEEP_ACTIVITY:
                intent.setClass(context, BatterySleepActivity.class);
                break;
            case DeviceSettingsActivity.SPEAKER_VOLUME_ACTIVITY:
                intent.putExtra(CommonConstants.EXT_DEVICE_ITEM, deviceItem);
                intent.setClass(context, SpeakerVolumeActivity.class);
                break;
            case DeviceSettingsActivity.PIR_ACTIVITY:
                intent.setClass(context, PIRSensitivityActivity.class);
                break;
            case DeviceSettingsActivity.REBOOT_ACTIVITY:
                intent.setClass(context, RebootActivity.class);
                break;

            case DAY_NIGHT_AUTO_ACTIVITY:
                intent.putExtra(CommonConstants.EXT_DEVICE_ITEM, deviceItem);
                intent.setClass(context, DoubleLightAutoActivity.class);
                break;
            case DOUBLE_LIGHT_ACTIVITY:
                intent.putExtra(CommonConstants.EXT_DEVICE_ITEM, deviceItem);
                intent.setClass(context, DoubleLightActivity.class);
                break;
        }
        try {
            context.startActivity(intent);
        } catch (Throwable exStart) {
            TGLog.e(TAG, "error while start activity: " + intent);
        }
    }

    public static void updateSettings(DeviceFeature deviceFeature, DeviceSettingsInfo deviceSettingsInfo) {
        if (deviceFeature != null) {
            Box<DeviceFeature> deviceFeatureBox = DeviceFeatureObjectBox.getDeviceFeature();
            assert deviceFeatureBox != null;
            deviceFeatureBox.put(deviceFeature);
        }
        if (deviceSettingsInfo != null) {
            Box<DeviceSettingsInfo> deviceSettingsInfoBox = ObjectBoxUtil.getDeviceSettingsInfo();
            deviceSettingsInfo.updated_at = System.currentTimeMillis();
            assert deviceSettingsInfoBox != null;
            deviceSettingsInfoBox.put(deviceSettingsInfo);
        }
    }

    public interface DeviceSettingsReceiver{
        void onReceive(Context context, Intent intent);
    }


    public static class DeviceSettingsBroadcastReceiver extends BroadcastReceiver {
        public void setDeviceSettingsReceiver(DeviceSettingsReceiver deviceSettingsReceiver) {
            mDeviceSettingsReceiver = deviceSettingsReceiver;
        }
        private DeviceSettingsReceiver mDeviceSettingsReceiver;
        @Override
        public void onReceive(Context context, Intent intent) {
            if (mDeviceSettingsReceiver != null){
                mDeviceSettingsReceiver.onReceive(context, intent);
            }
        }
    }

    public static DeviceSettingsBroadcastReceiver registerReceiver(Context context, DeviceSettingsReceiver deviceSettingsReceiver){
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(DeviceSettingsActivity.ACTION_DEVICE_SETTINGS);
        DeviceSettingsBroadcastReceiver receiver = new DeviceSettingsBroadcastReceiver();
        receiver.setDeviceSettingsReceiver(deviceSettingsReceiver);
        context.registerReceiver(receiver, intentFilter);
        return receiver;
    }
}
